package cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.reject.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.CompleteTaskRequest;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import org.junit.Assert;

public class RejectAnalyzeWorkReportFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);

        String firstAuditUserId = startWorkFlowRequest.contextData.getString("firstAuditUserId");

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
        workFlowTaskQuery.instanceStatusList.add(WorkFlowInstanceStatus.Running.status);

        //一级审批人拒绝
        {
            workFlowTaskQuery.assignerList.add(firstAuditUserId);
            WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);

            CompleteTaskRequest completeTaskRequest = new CompleteTaskRequest();
            completeTaskRequest.taskId = workFlowTaskResponse.taskId;
            completeTaskRequest.taskType = TaskType.REJECT;
            completeTaskRequest.rejectReason = "不同意";
            boolean instanceFinished = quickWorkFlow.completeTask(completeTaskRequest);
            Assert.assertTrue("实例当前并未结束", instanceFinished);
        }
    }

    @Override
    public String name() {
        return "拒绝分析工作报告审批";
    }

}
